package com.weisy.signin.ui.activity;

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.squareup.picasso.Picasso;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.Plan;
import com.weisy.signin.bean.Teacher;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.GsonUtils;
import com.weisy.signin.utils.ImageUtils;
import com.weisy.signin.utils.SpUtils;
import com.weisy.signin.widget.CircleTransform;
import com.weisy.signin.widget.LoadingDialog;
import com.yinshan.calendar.PopCalendar;
import com.yinshan.calendar.utils.DateTimeHelper;
import com.yinshan.calendar.utils.Utils;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

import static com.weisy.signin.comm.Comm.ROUTE_ADD_PLAN;

/**
 * @author weisy
 * @function 新增计划
 * @date 2019/3/27 21:57
 */
@Route(path = ROUTE_ADD_PLAN)
public class AddPlanActivity extends BaseAcativity {
    @BindView(R.id.img_add_head)
    ImageView imgHead;
    @BindView(R.id.txt_add_id)
    TextView txtId;
    @BindView(R.id.txt_real_name)
    TextView txtName;
    @BindView(R.id.txt_add_time)
    TextView txtTime;

    @BindView(R.id.edit_add_title)
    EditText editTitle;
    @BindView(R.id.edit_add_local)
    EditText editLocal;
    @BindView(R.id.edit_add_info)
    EditText editInfo;

    private Plan plan = new Plan();
    private Teacher teacher;

    private PopCalendar popCalendar;

    @Override
    public void findView() {
        setContentView(R.layout.actiivty_add_plan);
    }

    @Override
    public void initView() {
        teacher = GsonUtils.str2Bean((String) SpUtils.get(context, Comm.SP_PERION, ""), Teacher.class);
        if (null != teacher) {
            txtId.setText(teacher.getTeachId());
            if (TextUtils.isEmpty(teacher.getName())) {
                txtName.setText("未设置");
            } else {
                txtName.setText(teacher.getName());
            }
            if (!TextUtils.isEmpty(teacher.getImgUrl())) {
                Bitmap bp = ImageUtils.str2Bitmap(teacher.getImgUrl());
                Picasso.get().load(ImageUtils.bit2Uri(context, bp)).transform(new CircleTransform())
                        .into(imgHead);
            }
        }
        popCalendar = new PopCalendar(context)
                .setSelectColor(R.color.colorBlue)
                .setOnResultListener(new PopCalendar.OnResultListener() {
                    @Override
                    public void onResult(int i, int i1, int i2) {
                        Calendar c = Calendar.getInstance();
                        c.set(Calendar.YEAR, i);
                        c.set(Calendar.MONTH, i1 - 1);
                        c.set(Calendar.DAY_OF_MONTH, i2);
                        plan.setDate(DateTimeHelper.formatToString(c.getTime(), DateTimeHelper.FORMAT_DAY));
                        txtTime.setText(plan.getDate());
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.btn_add_submit, R.id.txt_add_time, R.id.img_add_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_add_submit:
                submit();
                break;
            case R.id.img_add_close:
                finish();
                break;

            case R.id.txt_add_time:
                if (null != popCalendar) {
                    popCalendar.show(view);
                }
                break;
        }
    }

    /**
     * 提交日程
     */
    private void submit() {
        plan.setTeachId(teacher.getTeachId());
        plan.setTitle(editTitle.getText().toString().trim());
        plan.setLocal(editLocal.getText().toString().trim());
        plan.setInfo(editInfo.getText().toString().trim());
        plan.setTime(DateTimeHelper.formatToString(Calendar.getInstance().getTime(),DateTimeHelper.FORMAT_24));
        if (TextUtils.isEmpty(plan.getDate())) {
            ToastUtils.show("活动时间不能为空");
            return;
        }
        if (TextUtils.isEmpty(plan.getTitle())) {
            ToastUtils.show("活动标题不能为空");
            return;
        }
        final LoadingDialog loadingDialog = new LoadingDialog.Builder(context)
                .setBgDark(true)
                .setMsg("提交中...")
                .create();
        loadingDialog.show();
        new Handler().postDelayed(() -> {
            plan.save();
            loadingDialog.dismiss();
            new LoadingDialog.Builder(context)
                    .setType(LoadingDialog.TYPE_SUCCESS)
                    .setMsg("提交成功")
                    .setBgDark(true)
                    .setOnDismissListener((dialog, which) -> {
                        dialog.dismiss();
                        finish();
                    })
                    .create().show();
        }, 1000);
    }
}
